package com.example.bailin.giftsay.guide;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.guide.adapter.GuideAdapter;
import com.example.bailin.giftsay.guide.adapter.PopupAdapter;
import com.example.bailin.giftsay.main.SearchActivity;
import com.example.bailin.giftsay.tool.MyApp;

/**
 * Created by bailin on 16/8/15.
 * <p/>
 * 指南的主Fragment
 */
public class GuideFragment extends BaseFragment implements View.OnClickListener, PopupClick {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ImageView ivPopup;
    private PopupWindow popupWindow;
    private RelativeLayout rlClick;
    private TextView tvGuide;
    private ImageView ivSearch;
    private PopupAdapter popupAdapter;

    @Override
    protected int setLayout() {
        return R.layout.fragment_guide;
    }

    @Override
    protected void initView() {
        tabLayout = bindView(R.id.tablayout_guide_id);
        viewPager = bindView(R.id.viewpager_guide_id);
        ivPopup = bindView(R.id.iv_popup_id);
        rlClick = bindView(R.id.rl_click_id);
        tvGuide = bindView(R.id.tv_guide_id);
        ivSearch = bindView(R.id.iv_guide_search);
        ivSearch.setOnClickListener(this);
        rlClick.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        popupAdapter = new PopupAdapter();
        GuideAdapter adapter = new GuideAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                popupAdapter.setIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_click_id:
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    return;
                } else {
                    initmPopupWindowView();
                    // tablayout 消失
                    tabLayout.setVisibility(View.INVISIBLE);
                    tvGuide.setVisibility(View.VISIBLE);
                    ivPopup.setImageResource(R.mipmap.arrow_grey_up);

                    rlClick.setBackground(null);
                    // 点击popupwindow其他地方会消失
                    popupWindow.setFocusable(true);
                }
                popupWindow.showAsDropDown(v, 0, 5);
                break;
            case R.id.iv_guide_search:
                Intent intent = new Intent(MyApp.getContext(), SearchActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void initmPopupWindowView() {
        View customView = getLayoutInflater(new Bundle()).inflate(R.layout.item_rv_popup, null);
        RecyclerView rvPopup = (RecyclerView) customView.findViewById(R.id.rv_popup_id);
        GridLayoutManager manager = new GridLayoutManager(MyApp.getContext(), 4);

        rvPopup.setLayoutManager(manager);
        popupAdapter.setPopupClick(this);

        rvPopup.setAdapter(popupAdapter);

        popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        // 监听PopupWindow消失
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ivPopup.setImageResource(R.mipmap.arrow_grey_down);
                tabLayout.setVisibility(View.VISIBLE);
                tvGuide.setVisibility(View.INVISIBLE);
                rlClick.setBackgroundColor(0xffffffff);
            }
        });

        customView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });
    }

    @Override
    public void itemOnClick(int position) {
        popupWindow.dismiss();
        ivPopup.setImageResource(R.mipmap.arrow_grey_down);
        // 滚动到TabLayou对应的位置但是不会改变当前位置的数据
        tabLayout.setScrollPosition(position, 0, true);
        // 改变TabLayou当前位置的数据
        tabLayout.getTabAt(position).select();
    }
}